package com.huifaapp.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Specialactivity;
import com.huifaapp.mall.activity.Tmall_homepage2Fragment;
import com.huifaapp.mall.activity.TodaysSelectionFragment;
import com.huifaapp.mall.bean.JDcomclassifyBean;
import com.huifaapp.mall.uitils.Contig;

import java.util.List;


/**
 * Created by duanyu on 2018/1/17.
 */

public class Tmall_popwinAdapter extends RecyclerView.Adapter<Tmall_popwinAdapter.ViewHolder>implements  View.OnClickListener {
    private List<JDcomclassifyBean.DataBean>list;
    private Context context;
    //当前Item被点击的位置
    private int currentItem;
    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }
    public Tmall_popwinAdapter(List<JDcomclassifyBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View recyclerViewItem = LayoutInflater.from(context).inflate(R.layout.itme_popwin,parent, false);
        recyclerViewItem.setOnClickListener(this);
        return new ViewHolder(recyclerViewItem);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
          holder.itme_popwin_classify.setText(list.get(position).getName());
          holder.itemView.setTag(position);
        //解决RecyclerView和checkBox问题
        holder.itme_popwin_classify.setTag(position);
        if (Tmall_homepage2Fragment.hmap.containsKey(position)) {
            holder.itme_popwin_classify.setSelected(Tmall_homepage2Fragment.hmap.get(position));
        } else {
            holder.itme_popwin_classify.setSelected(false);//true or false 都可以，看实际需求
        }
//        if (currentItem == position) {
//            Log.e("点击取的position选中",position+"");
//            //如果被点击，设置当前TextView被选中
//            holder.itme_popwin_classify.setSelected(true);
//            holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape);
//        } else {
//            Log.e("","");
//            Log.e("点击取的position未选中",position+"");
//            //如果没有被点击，设置当前TextView未被选中
//            holder.itme_popwin_classify.setSelected(false);
//            holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_button_mm);
//          }

            if(Contig.tabname.equals("推荐")){
                holder.itme_popwin_classify.setSelected(true);
                holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape);
                if (currentItem == position) {
                    Log.e("点击取的position选中",position+"");
                    //如果被点击，设置当前TextView被选中
                    holder.itme_popwin_classify.setSelected(true);
                    holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape);
                } else {
                    Log.e("","");
                    Log.e("点击取的position未选中",position+"");
                    //如果没有被点击，设置当前TextView未被选中
                    holder.itme_popwin_classify.setSelected(false);
                    holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_button_mm);
                }
            }else {
                if (Contig.tabname.equals(list.get(position).getName())) {
                    holder.itme_popwin_classify.setSelected(true);
                    holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape);
                }
            }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mMyItemclickListener != null) {
            mMyItemclickListener.itemclick(v, (Integer) v.getTag());
        }

    }

    //itme点击接口回调
    public interface MypopItemclickListener {
        void itemclick(View view, int position);
    }
    public MypopItemclickListener mMyItemclickListener;

    public void setmMyItemclickListener(MypopItemclickListener mMyItemclickListener) {
        this.mMyItemclickListener = mMyItemclickListener;
    }
    class ViewHolder extends RecyclerView.ViewHolder {


        private final TextView itme_popwin_classify;

        public ViewHolder(View itemView) {
            super(itemView);
            itme_popwin_classify = (TextView) itemView.findViewById(R.id.itme_popwin_classify);
        }
    }
}
